#version 440

layout(location = 0) in vec2 vPosition;
layout(location = 1) in vec2 vTexcoord;
layout(location = 2) in vec4 vColor;

layout(std140) uniform;

out vec4 Color;
out vec2 Texcoord1;
flat out int texIndex1;

struct DrawInstParams
{
	mat4 Mvp;
	mat4 ProjectionMatrix;
	mat4 ModelViewMatrix;
	mat3 NormalMatrix;
	vec4 Color;
};

layout(binding = 3) uniform ubDrawInstParams
{
	DrawInstParams drawInstParams[100];
};

struct LightParams
{
	vec4 ambient;
	vec4 diffuse;
	vec4 specular;
	vec4 position;
	vec4 halfVector;
	vec3 spotDirection;
	float spotExponent;
	float spotCutoff; // (range: [0.0,90.0], 180.0)
	float spotCosCutoff; // (range: [1.0,0.0],-1.0)
	float constantAttenuation;
	float linearAttenuation;
	float quadraticAttenuation;
};

struct MaterialParams
{
	vec4 ambient;
	vec4 diffuse;
	vec4 specular;
	vec4 emission;
	float shininess;
	float align1;
	vec2 textureOffset;
};

layout(binding = 2) uniform ubLight
{
	int lightIndex;
	MaterialParams materialParams;
	LightParams lightParams[8];
};

void main()
{
	//if (false)
	if (true)
	{
		vec4 pos=drawInstParams[gl_InstanceID].ModelViewMatrix * vec4(0,0,0,1);
		vec4 scale = vec4(length(drawInstParams[gl_InstanceID].ModelViewMatrix[0].xyz), length(drawInstParams[gl_InstanceID].ModelViewMatrix[1].xyz), length(drawInstParams[gl_InstanceID].ModelViewMatrix[2].xyz), 1);
		//gl_Position = drawInstParams[gl_InstanceID].ProjectionMatrix * scale * pos;
		gl_Position = drawInstParams[gl_InstanceID].ProjectionMatrix * vec4(pos.xyz + vec3(vPosition,0)*scale.xyz,pos.w);
	}
	else
	{
		gl_Position = drawInstParams[gl_InstanceID].Mvp * vec4(vPosition,0,1);
	}
	//gl_Position = vec4(vPosition/600.0f,1);
	Texcoord1=vTexcoord + materialParams.textureOffset;
	//texIndex1=vTextureIndex;
	Color=vColor * drawInstParams[0].Color;
}